<template>
  <div class="app-container">


    <!-- 首页内容 -->
    <div class="page-container-wrap">
      <div class="page-container">
        <div class="recent-posts">
          <div class="announcement background-opacity" :class=" sortPage ?'announcement-max-width':''">
            <div class="announcement-content">
              <el-select
                v-model="isSearchMode"
                placeholder="请选择行数"
                style="width: 70px; margin-right: 10px;border: white"
              >
                <el-option
                  v-for="item in divType"
                  :key="item"
                  :label="item.type"
                  :value="item.value"
                ></el-option>
              </el-select>
              <template v-if="!isSearchMode">
                <i class="fa fa-volume-up" aria-hidden="true"></i>
                <div>
                  <div v-for="(notice, index) in notices" :key="index">
                    {{ notice }}
                  </div>
                </div>
              </template>
              <template v-else>
                <input
                  v-model="pagination.articleTitle"
                  class="search-input"
                  placeholder="输入关键词搜索文章"
                />
                <i
                  class="fa fa-search search-icon"
                  style="margin-left: 20px"
                  @click="selectSubList"
                  :class="{'active': isSearchMode}"
                ></i>
              </template>
            </div>
          </div>
            <model-type ref="modelTypeRef"  :model-name="route.query.type"  :sortId="route.query.sortId" :rows="articleRows" :columns="articleColumns" :key="modelKey" :article-title="pagination.articleTitle"></model-type>

        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {listArticle,selectArticlesByType} from "@/api/weblog/article";
import modelType from "@/views/weblogFront/util/modelType.vue"

import {getCurrentInstance, nextTick, onMounted, reactive, ref, shallowRef, watch} from "vue";
import {useRoute, useRouter} from "vue-router";
import {getSort} from "@/api/weblog/sort";
const { proxy } = getCurrentInstance();
const key = ref(1);
const notices = ref(["公告一"]);
const open = ref(false);
const sortPage = ref(false);
const articles = ref([]);
const pagination = ref({
  current: 1,
  size: 10,
  total: 0,
  searchKey: "",
  sortId: null,
  labelId:"",
  articleSearch: "",
  articleTitle:""
})
const modelTypeRef=ref(1);
const route = useRoute();
const router = useRouter();
const modelKey = ref(1);
const articleRows = ref(0);
const articleColumns = ref(0);
const articleTitle = ref("");
const isSearchMode = ref(false);
const divType=ref([
  {type:"搜索",value:true},{type: "公告",value:false}]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);



const toggleSearch = () => {
  isSearchMode.value = !isSearchMode.value;
}
async function getArticles() {
  let axiosResponse = await listArticle(pagination.value);
    articles.value = axiosResponse.rows;
    pagination.value.total = axiosResponse.total;
  await nextTick(() => {
    modelKey.value = modelKey.value + 1
  })

}


onMounted(() => {
  refreshArticle()
})

watch(() => route.query.sortId, (newId, oldId) => {
  // 当路由参数变化时执行的逻辑
  if (newId !== oldId) {
    refreshArticle(newId);
  }
});

const selectSubList = async () => {
  modelTypeRef.value.getSubArticleList();

}

const refreshArticle =async () => {

  let response = await getSort(route.query.sortId);
  articleRows.value = response.data.articleRows
  articleColumns.value = response.data.articleColumns

  pagination.value = {
    current: 1,
    size: 10,
    total: 0,
    searchKey: "",
    labelId: route.query.labelId,
    sortId: route.query.sortId,
    articleTitle: route.query.articleTitle,
    articleSearch: ""
  };
  articleTitle.value = route.query.articleTitle
  await getArticles();
}

// getList();
</script>
<style scoped>

.toolbar-content {
  width: 100%;
  height: 600px !important;
  /* 固定位置，不随滚动条滚动 */
  position: fixed;
  z-index: 100;
  /* 禁止选中文字 */
  user-select: none;
  transition: all 0.3s ease-in-out;
}



.page-container-wrap {
  background: var(--indexbg);
  position: relative;
  min-height: 100vh;
}

.page-container {
  display: flex;
  justify-content: center;
  width: 90%;
  padding: 60px 20px 40px 20px;
  margin: 0 auto;
  flex-direction: row;
}

.recent-posts {
  width: 70%;
}


.announcement {
  padding: 22px;
  border: 1px dashed var(--lightGray);
  color: var(--greyFont);
  border-radius: 10px;
  margin: 40px auto 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
}

.announcement-content {
  flex: 1;
  display: flex;
  align-items: center;
}

.search-icon {
  margin-left: 15px;
  font-size: 18px;
  color: var(--themeBackground);
  cursor: pointer;
  transition: all 0.3s;
}

.search-icon:hover {
  transform: scale(1.2);
}

.search-icon.active {
  color: var(--red);
}

.search-input {
  flex: 1;
  margin-left: 15px;
  padding: 5px 10px;
  border: 1px solid var(--lightGray);
  border-radius: 15px;
  background: transparent;
  color: var(--greyFont);
  outline: none;
}

.search-input:focus {
  border-color: var(--themeBackground);
}

.announcement i {
  color: var(--themeBackground);
  font-size: 22px;
  margin: auto 0;
  animation: scale 0.8s ease-in-out infinite;
}
.announcement-max-width{
  max-width: 780px;
}

.announcement div div {
  margin-left: 20px;
  line-height: 30px;
}

.aside-content {
  width: calc(30% - 40px);
  user-select: none;
  margin-top: 40px;
  margin-right: 40px;
  max-width: 300px;
  float: right;
}

.pagination-wrap {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.pagination {
  padding: 13px 15px;
  border: 1px solid var(--lightGray);
  border-radius: 3rem;
  color: var(--greyFont);
  width: 100px;
  user-select: none;
  cursor: pointer;
  text-align: center;
}

.pagination:hover {
  border: 1px solid var(--themeBackground);
  color: var(--themeBackground);
  box-shadow: 0 0 5px var(--themeBackground);
}

@media screen and (max-width: 1100px) {
  .recent-posts {
    width: 100%;
  }

  .page-container {
    width: 100%;
  }
}

@media screen and (max-width: 1000px) {

  .page-container {
    /* 文章栏与侧标栏垂直排列 */
    flex-direction: column;
  }

  .aside-content {
    width: 100%;
    max-width: unset;
    float: unset;
    margin: 40px auto 0;
  }
}

@media screen and (max-width: 768px) {

  h1 {
    font-size: 35px;
  }
}

.app-container {
  padding: 0px !important;
}
:deep(.el-input__wrapper ) {
  box-shadow: 0 0 0 1px #fff inset;
}

</style>
